$(function(){
	var   $nav = $('#nav'), aRules = [];

	//create the ul
	$('section').each(function(){
		var  $this = $(this)
		  , sTitle = $this.attr('data-nav') || $this.find('h2:first').text()
		  ,    sId = $this.attr('id').split('-').pop(); //story-header -> header

		aRules.push('body.section-' + sId + ' #nav li.' + sId + ' a');
		$nav.append('<li class="' + sId + '"><h1><span>' + sTitle + 
			'</span></h1><a href="#' + sId + '">' + sId + '</a></li>');
	});


    //add blog link to menu manually
    var $url = $(location).attr('href');
    var $lang = 'en';
    var $blog = 'Blog';
    if ($url.toLowerCase().indexOf('/bg/') >= 0) { $lang = 'bg'; $blog = 'Блог'; }

    $nav.append('<li class="Blog"><h1><span>' + $blog + '</span></h1>' +
        '<a href="http://blog.forafreeworld.com" target="_blank">' + $blog + '</a></li>');


	$nav.on('click', 'a', function(){
			scrollToSection($(this).attr('href').substr(1)); //to remove #
		});

		//scrollToSection for all a's in document
	$('body').on('click', 'a', function(ev){
		var href = this.getAttribute('href') || '';

		if( href.indexOf('#') === 0){
			ev.preventDefault();
			scrollToSection(href.substr(1));
		}
	} );
});